;layui.define(function (e) {
    layui.use(['index', 'form', 'jquery', 'http','common', 'laytpl'], function () {

        var $ = layui.$
            , http = layui.http
            , form = layui.form
            , common = layui.common
            , laytpl = layui.laytpl;

        var urls = {
            getRoleMenuPermissionData: "/core/role/menu/permission/getRoleMenuPermissionData?roleId=" + $('#roleId').val() + "&menuId=" + $('#menuId').val(),
            modifyData: "/core/role/menu/permission/modifyData"
        };

        http.get({
            loading: true,
            loadingText: '',
            url: urls.getRoleMenuPermissionData,
            success: function (res) {
                var getTpl = $('#permission').html()
                    , view = $('#permissionList');
                laytpl(getTpl).render(res.data, function (html) {
                    view.html(html);
                    form.render('checkbox');
                });

            }
        })
        form.on("submit(modifyData)", function (data) {
            var permissionIds = [];
            $('input[type=checkbox]:checked').each(function (i, item) {
                permissionIds.push($(item).val());
            });
            data.field['permissionId'] = permissionIds.join(',');
            http.post({
                loading: true,
                loadingText: '数据提交中,请稍后...',
                url: urls.modifyData,
                data: data.field,
                success: function (res) {
                    parent.fn.refresh(false);
                    common.showSuccessTips('角色菜单权限保存成功!')
                }
            });
            return false;
        });

    });

    e("core/role_menu_permission", {});
});
